/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.boudreaulocke.model.persistence;

import java.util.Random;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.transaction.annotation.Transactional;

import com.boudreaulocke.model.PhoneNumber;

/**
 * Spring bean that initializes DAO storage to hold a given set of initial
 * objects for demo purposes.
 * 
 * @author Jonathan Locke
 */
public class PersonDaoInitializer implements InitializingBean {

	/**
	 * DAO injected by Spring
	 */
	private IPersonDao dao;

	/**
	 * Spring injection point
	 * 
	 * @param dao
	 *            Dao to inject
	 */
	public void setPersonDao(IPersonDao dao) {
		this.dao = dao;
	}

	/**
	 * Called by Spring to initialize
	 */
	@Transactional
	public void afterPropertiesSet() {

		final String[] names = { "Marlon Brando", "Dustin Hoffman",
				"Meryl Streep", "Johnny Depp", "Robert De Niro",
				"Charlie Chaplin", "Gene Hackman", "Judy Dench",
				"Daniel Day-Lewis", "Anthony Hopkins", "Don Cheadle",
				"Alan Arkin", "David Strathairn", "Philip Seymour Hoffman",
				"James Dean", "Ian McKellen", "Bill Pullman", "Will Ferrell",
				"Juliette Lewis", "Vivien Leigh", "Orson Welles",
				"Humphrey Bogart", "Lauren Bacall", "Geoffrey Rush",
				"Nicholas Cage", "Jack Lemmon", "Gregory Peck", "Heath Ledger",
				"Jennifer Connelly", "Ben Kingsley", "John Cusack",
				"Bruce Willis", "William H. Macy", "Patricia Clarkson",
				"Edward Norton", "John Malkovich", "Brad Pitt", "Alec Baldwin",
				"Forest Whitaker", "Bill Murray", "Helena Bonham Carter",
				"Matt Damon", "Kevin Spacey", "Tommy Lee Jones",
				"George Clooney", "Ed Harris", "Jeff Bridges", "Emma Thompson",
				"Morgan Freeman", "Kathy Bates", "Robert Duvall", "Sean Penn",
				"Al Pacino", "Jack Nicholson", "Paul Newman", "Peter O'Toole",
				"Michael Caine", "Denzel Washington", "Clint Eastwood" };

		if (dao.query().countMatches() <= 0) {
			System.out.println("Adding people to database");
			for (String name : names) {
				PersistentPerson person = new PersistentPerson();
				person.setName(name);
				person.setPhoneNumber(randomPhoneNumber());
				dao.create(person);
			}
		}

		System.out.println("Database has " + dao.query().countMatches() + " people:");
		for (PersistentPerson person : dao.query().matches()) {
			System.out.println(person);
		}
	}

	/**
	 * @return A random phone number
	 */
	private PhoneNumber randomPhoneNumber() {
		return new PhoneNumber(randomIntegerBetween(100, 1000), 555,
				randomIntegerBetween(0, 1000));
	}

	/**
	 * @param min
	 *            Minimum value inclusive
	 * @param max
	 *            Maximum value exclusive
	 * @return The random value
	 */
	private int randomIntegerBetween(int min, int max) {
		return (Math.abs(new Random().nextInt()) % (max - min)) + min;
	}
}
